package cn.wolfcode.luowowo.wensite.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String idnex(Model model, HttpSession session) {
//        Object userInfo = session.getAttribute("userInfo");
//        System.out.println(userInfo);
        model.addAttribute("hotRegions", regionService.queryHotRegions());
        return "destination/index";
    }

    @RequestMapping("/getHotDestByRegionId")
    public Object getHotDestByRegionId(Model model, long regionId) {
        List<Destination> list = destinationService.getDestByRegionId(regionId);
        //然后将这个集合分为两半
        //左边的数据
        List<Destination> left = list.subList(0, list.size() / 2 + 1);
        //右边的数据
        List<Destination> right = list.subList(list.size() / 2 + 1, list.size());
        model.addAttribute("leftDests", left);
        model.addAttribute("rightDests", right);
        model.addAttribute("regionId", regionId);

        return "destination/hotdestTpl";
    }

    //查看某个目的地的明细
    @RequestMapping("/guide")
    public Object guide(Model model, long id) {
        //toasts
        List<Destination> toasts = destinationService.getToasts(id);
        //这样拼接的话,先减删掉当前吐司导航地址,不然他会拼接2个当前目的地
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", destination);

        List<StrategyCatalog> catalogs = strategyCatalogService.queryByDestId(id);
        model.addAttribute("catalogs", catalogs);

        List<StrategyDetail> strategyDetails = strategyDetailService.queryViewNumTop3(id);
        model.addAttribute("strategyDetails", strategyDetails);
        return "destination/guide";
    }

    @RequestMapping("/surveyPage")
    public Object surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", destination);
        return "destination/survey";
    }

    @RequestMapping("/survey")
    public Object survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {
        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.queryByDestId(qo.getDestId());
        model.addAttribute("catalogs", catalogs);
        //catalog
        StrategyCatalog catalog = null;
        for (StrategyCatalog c : catalogs) {
            if (c.getId() == qo.getCatalogId()) {
                catalog = c;
                model.addAttribute("catalog", catalog);
            }
        }
        if (catalog != null) {
            List<StrategyDetail> details = catalog.getDetails();
            if (details != null && details.size() > 0) {
                StrategyDetail detail = details.get(0);
                detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
                model.addAttribute("detail", details.get(0));
            }
        }
        return "destination/surveyTpl";
    }

    //查看游记
    @RequestMapping("/travels")
    public String travels(Model model, @ModelAttribute("qo") TravelQuery qo) {
        model.addAttribute("pageInfo",travelService.query(qo));
        return "destination/travelTpl";
    }
}
